#include <utility>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#include <qlogging.h>
#include <QOpenGLTexture>
#endif
class MyEllipse {
public:
    MyEllipse(float xPos, float major, float minor, float angle, QImage textureImage)
        : xPos(xPos), major(major), minor(minor), angle(angle), textureImage(std::move(textureImage)) {
        if (!this->textureImage.isNull()) {
            texture = new QOpenGLTexture(this->textureImage);
            texture->setMinificationFilter(QOpenGLTexture::Linear);
            texture->setMagnificationFilter(QOpenGLTexture::Linear);
            texture->setWrapMode(QOpenGLTexture::ClampToEdge);
            qDebug() << "Texture loaded successfully from QImage";
        } else {
            qDebug() << "Failed to load texture: QImage is null";
        }
    }

    ~MyEllipse() {
        if (texture) {
            delete texture;
            texture = nullptr;
        }
    }

    MyEllipse(const MyEllipse&) = delete;
    MyEllipse& operator=(const MyEllipse&) = delete;

    MyEllipse(MyEllipse&& other) noexcept
        : xPos(other.xPos), major(other.major), minor(other.minor),
          angle(other.angle), textureImage(std::move(other.textureImage)),
          texture(other.texture) {
        other.texture = nullptr;
    }

    MyEllipse& operator=(MyEllipse&& other) noexcept {
        if (this != &other) {
            if (texture) {
                delete texture;
                texture = nullptr;
            }
            xPos = other.xPos;
            major = other.major;
            minor = other.minor;
            angle = other.angle;
            textureImage = std::move(other.textureImage);
            texture = other.texture;
            other.texture = nullptr;
        }
        return *this;
    }

    void draw() {
        glPushMatrix();
        glTranslatef(xPos, 0.0f, 0.0f);

        if (texture) {
            texture->bind();
            glEnable(GL_TEXTURE_2D);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glColor3f(1.0f, 1.0f, 1.0f);

            // 应用旋转
            glRotatef(-angle, 1.0f, 0.0f, 0.0f);

            drawEllipseWithTriangleFan();

            texture->release();
            glDisable(GL_TEXTURE_2D);
            glDisable(GL_BLEND);
        } else {
            // 如果没有纹理，绘制纯色椭圆
            glRotatef(-angle, 1.0f, 0.0f, 0.0f);
            drawSolidEllipse();
        }

        // 绘制红色虚线的轮廓、长轴和短轴
        glColor3f(1.0f, 0.0f, 0.0f); // 红色
        glEnable(GL_LINE_STIPPLE);
        glLineStipple(1, 0x00FF); // 虚线模式
        glLineWidth(2.0f); // 设置线宽为 2.0f

        // 绘制椭圆轮廓
        glBegin(GL_LINE_LOOP);
        for (int i = 0; i <= 360; i++) {
            float rad = i * M_PI / 180.0f;
            float y = major * cos(rad);
            float z = minor * sin(rad);
            glVertex3f(0.0f, y, z);
        }
        glEnd();

        // 绘制长轴
        glBegin(GL_LINES);
        glVertex3f(0.0f, -major, 0.0f);
        glVertex3f(0.0f, major, 0.0f);
        glEnd();

        // 绘制短轴
        glBegin(GL_LINES);
        glVertex3f(0.0f, 0.0f, -minor);
        glVertex3f(0.0f, 0.0f, minor);
        glEnd();

        glDisable(GL_LINE_STIPPLE); // 禁用虚线模式

        glPopMatrix();
    }

    void drawSolidEllipse() {
        int segments = 36;

        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.0f, 0.0f, 0.0f); // 中心点

        for (int i = 0; i <= segments; i++) {
            float angle = i * 2 * M_PI / segments;
            float y = major * cos(angle);
            float z = minor * sin(angle);
            glVertex3f(0.0f, y, z);
        }
        glEnd();
    }

    void drawEllipseWithTriangleFan() {
        int segments = 36;

        glBegin(GL_TRIANGLE_FAN);

        // 中心点
        glTexCoord2f(0.5f, 0.5f);
        glVertex3f(0.0f, 0.0f, 0.0f);

        for (int i = 0; i <= segments; i++) {
            float angle = i * 2 * M_PI / segments;
            float y = major * cos(angle);
            float z = minor * sin(angle);
            float u = 0.5f - 0.5f * sin(angle);
            float v = 0.5f - 0.5f * cos(angle);

            glTexCoord2f(u, v);
            glVertex3f(0.0f, y, z);
        }

        glEnd();
    }

    float xPos, major, minor, angle;
    QImage textureImage;
    QOpenGLTexture *texture = nullptr;
};
